import { _decorator, Button, Component, Label, Prefab, UIOpacity, Node, instantiate } from 'cc';
import { Databus } from '../core/Databus';
import { updateSetting } from '../services/Setting';
import { updateUserGoldCoin } from '../services/UserInfo';
import { UniformFunction } from '../platform/UniformFunction';
import { WxSdkManager } from '../platform/ad/WxSdkManager';


const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('EarnCoinPage')
export class EarnCoinPage extends Component {
  // 激励广告弹窗
  @property(Prefab)
  RewordVideoAds: Prefab = null;
  private rewordVideoInstance: Node = null;

  public backButton: Button = null;

  public loginSignButton: Button = null;
  public shareGroupButton: Button = null;
  public freeTimeButton: Button = null;
  public watchAdsButton: Button = null;
  onLoad(): void {
    this.backButton = this.node.getChildByName('BackButton').getComponent(Button);
    this.loginSignButton = this.node.getChildByName('LoginSignButton').getComponent(Button);
    this.shareGroupButton = this.node.getChildByName('ShareGroupButton').getComponent(Button);
    this.freeTimeButton = this.node.getChildByName('FreeTimeButton').getComponent(Button);
    this.watchAdsButton = this.node.getChildByName('WatchAdsButton').getComponent(Button);
    
    this.backButton.node.on(Button.EventType.CLICK, this.backPage, this);

    this.loginSignButton.node.on(Button.EventType.CLICK, this.signLogin, this,true);
    this.freeTimeButton.node.on(Button.EventType.CLICK, this.startFreeTime, this,true);
    this.shareGroupButton.node.on(Button.EventType.CLICK, this.startShareGroup, this,true);
    this.watchAdsButton.node.on(Button.EventType.CLICK, this.startWatchAds, this,true);
  }
  public start() {
  }


  protected onEnable(): void {
   // console.log("555555555555555")
   WxSdkManager.showRecommend();
    WxSdkManager.toggleCustomCol33Ad(true)
  }

  protected onDisable(): void {
  //  console.log("777777777777777777")
    WxSdkManager.toggleCustomCol33Ad(false)
  }

  update(deltaTime: number) {
    this.node.getChildByName('LoginSignButton').getComponent(UIOpacity).opacity = 255;
    this.node.getChildByName('ShareGroupButton').getComponent(UIOpacity).opacity = 255;
    this.node.getChildByName('FreeTimeButton').getComponent(UIOpacity).opacity = 255;
    if (databus.earnCoin.sign >= 1) {
      this.node.getChildByName('LoginSignButton').getComponent(UIOpacity).opacity = 50;
    }
    if (databus.earnCoin.shareGroup >= 1) {
      this.node.getChildByName('ShareGroupButton').getComponent(UIOpacity).opacity = 50;
    }
    if (databus.earnCoin.freeTime >= 3) {
      this.node.getChildByName('FreeTimeButton').getComponent(UIOpacity).opacity = 50;
    }
    this.node.getChildByName('FreeTimeButton').getChildByName('TimeLabel').getComponent(Label).string = `X${3 - databus.earnCoin.freeTime}`;
  }
  public signLogin() {
    if (databus.earnCoin.sign >= 1) { return ;}
    databus.rad.adType = 1;
    this.showRewordVideoNode();
  }
  public startShareGroup() {
    if (databus.earnCoin.shareGroup >= 1) { return; }
    new UniformFunction().shareToGroup();
    const that = this;
    setTimeout(() => {
      databus.earnCoin.shareGroup += 1;
      that.updateUserCoin(6000);
      that.updateEarnCoin();
      new UniformFunction().showInfoToast('领取成功', 3000);
    }, 3000);
  }
  public startFreeTime() {
    if (databus.earnCoin.freeTime >= 3) { return; }
    databus.rad.adType = 2;
    this.showRewordVideoNode();
  }
  public startWatchAds() {
    databus.rad.adType = 0;
    this.showRewordVideoNode();
  }
  
  public backPage() {
    databus.sceneStack.backScene();
  }

  private updateUserCoin(diffCoin: number) {
    updateUserGoldCoin(databus.selfInfo.userId, databus.selfInfo.goldCoin, diffCoin);
    databus.selfInfo.addGoldCoin(diffCoin);
  }

  private updateEarnCoin() {
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 1, 'coin', JSON.stringify(databus.earnCoin));
  }

  private showRewordVideoNode() {
    if (!this.rewordVideoInstance) {
      this.rewordVideoInstance = instantiate(this.RewordVideoAds);
      this.rewordVideoInstance.setParent(this.node.getChildByName("EarnCoinRoot"));
    
    }

    this.rewordVideoInstance.active = true;
  }
}